package com.persagy.web.quartz;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.constant.quartz.QuartzConstant;
import com.persagy.ems.dto.quartz.JobParams;
import com.persagy.ems.pojo.quartz.ScheduleJob;
import com.persagy.service.quartz.ScheduleJobService;

@Service("ScheduleTaskService")
@SuppressWarnings({ "rawtypes","unchecked" })
public class ScheduleTaskService extends BaseBusinessService<JobParams> implements BusinessService {

	@Resource
	private ScheduleJobService scheduleJobService;

	@Override
	public void handle(JobParams dto, List content) throws Exception {
		String operateType = dto.getOperateType();
		ScheduleJob job = new ScheduleJob();
		initJobParams(dto, job);
		if (StringUtils.isBlank(operateType)) {
			return;
		}
		switch (operateType.trim()) {
			case "getJobs":
				queryJob(job, content);
				break;
			case "getScheduleJobs":
				getScheduleJobs(content);
				break;
			case "getRunningJobs":
				getRunningJobs(content);
				break;
			case "insertJob":
				insertJob(job);
				break;
			case "updateJob":
				updateJob(job);
				break;
			case "deleteJob":
				deleteJob(job);
				break;
			case "startJob":
				startJob(job);
				break;
			case "pauseJob":
				pauseJob(job);
				break;
			case "resumeJob":
				resumeJob(job);
				break;
			case "triggerJob":
				triggerJob(job);
				break;
		}
	}

	private void initJobParams(JobParams dto, ScheduleJob job) {
		String jobId = dto.getJobId();
		String jobName = dto.getJobName();
		String jobStatus = dto.getJobStatus();
		String cronExpression = dto.getCronExpression();
		String jobClassName = dto.getJobClassName();
		String jobParams = dto.getJobParams();
		String desc = dto.getDesc();
		if (StringUtils.isNotBlank(jobId)) {
			job.setJobId(jobId);
		}
		if (StringUtils.isNotBlank(jobName)) {
			job.setJobName(jobName);
		}
		job.setJobGroup(QuartzConstant.JobGroup.EMS_C8);
		if (StringUtils.isNotBlank(jobStatus)) {
			job.setJobStatus(jobStatus);
		}
		if (StringUtils.isNotBlank(cronExpression)) {
			job.setCronExpression(cronExpression);
		}
		if (StringUtils.isNotBlank(jobClassName)) {
			job.setJobClassName(jobClassName);
		}
		if (StringUtils.isNotBlank(desc)) {
			job.setDesc(desc);
		}
		if (StringUtils.isNotBlank(jobParams)) {
			job.setJobParams(jobParams);
		}
	}

	private void queryJob(ScheduleJob job, List<ScheduleJob> content) throws Exception {
		List<ScheduleJob> list = scheduleJobService.getJobList(job);
		content.addAll(list);
	}

	private void getScheduleJobs(List<ScheduleJob> content) throws Exception {
		List<ScheduleJob> list = scheduleJobService.getScheduleJobs();
		content.addAll(list);
	}

	private void getRunningJobs(List<ScheduleJob> content) throws Exception {
		List<ScheduleJob> list = scheduleJobService.getRunningJobs();
		content.addAll(list);
	}

	private void insertJob(ScheduleJob job) throws Exception {
		scheduleJobService.insertJob(job);
	}

	private void updateJob(ScheduleJob job) throws Exception {
		ScheduleJob objectCriteria = new ScheduleJob();
		objectCriteria.setJobName(job.getJobName());
		objectCriteria.setJobGroup(job.getJobGroup());
		scheduleJobService.updateJob(objectCriteria, job);
	}

	private void deleteJob(ScheduleJob job) throws Exception {
		scheduleJobService.deleteJob(job);
	}

	private void startJob(ScheduleJob job) throws Exception {
		scheduleJobService.startJob(job);
	}

	private void pauseJob(ScheduleJob job) throws Exception {
		scheduleJobService.pauseJob(job);
	}

	private void resumeJob(ScheduleJob job) throws Exception {
		scheduleJobService.resumeJob(job);
	}
	private void triggerJob(ScheduleJob job) throws Exception {
		scheduleJobService.triggerJob(job);
	}

}
